package vq.codec.ui;

import javax.swing.JOptionPane;

import vq.codec.entities.Dat;
import vq.codec.entities.Dictionary;
import vq.codec.exception.SystemException;
import vq.codec.managers.VQCodecManager;
import vq.codec.managers.VQFileManager;

/**
 * Classe criada para representar o frame principal de entrada de dados para as operações de MSE
 * e PSNR e os resultados da mesma.
 *
 * @author Gutemberg Rodrigues
 * @since Outubro de 2009
 * @version 0.1
 */

public class InputScreenCalc extends javax.swing.JFrame {

	private VQFileManager fileManager;
	private VQCodecManager codedManager;
	
    public InputScreenCalc() {
        initComponents();
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        fileManager = VQFileManager.getInstance();
        codedManager = VQCodecManager.getInstance();
    }

    @SuppressWarnings("unchecked")

    private void initComponents() {

        labelSource = new javax.swing.JLabel();
        labelEncoded = new javax.swing.JLabel();
        txtSource = new javax.swing.JTextField();
        txtEncoded = new javax.swing.JTextField();
        labelInformation = new javax.swing.JLabel();
        buttonCodec = new javax.swing.JButton();
        buttonClean = new javax.swing.JButton();
        labelInformation2 = new javax.swing.JLabel();
        panelResult = new javax.swing.JPanel();
        labelMSE = new javax.swing.JLabel();
        labelPSNR = new javax.swing.JLabel();
        txtMSE = new javax.swing.JTextField();
        txtPSNR = new javax.swing.JTextField();
        labelResult = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelSource.setText("Informe o caminho do arquivo Dat Source:");

        labelEncoded.setText("Informe o caminho do arquivo Dat Gerado:");

        txtSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSourceActionPerformed(evt);
            }
        });

        txtEncoded.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEncodedActionPerformed(evt);
            }
        });

        labelInformation.setForeground(new java.awt.Color(153, 153, 153));
        labelInformation.setText("exemplo:  C:\\Users\\Administrador\\Desktop\\nomeArquivo.dat");

        buttonCodec.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonCodec.setIcon(new javax.swing.ImageIcon("./images/iconeConfirmar.gif")); 
        buttonCodec.setText("Calcular");
        buttonCodec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCodecActionPerformed(evt);
            }
        });

        buttonClean.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonClean.setIcon(new javax.swing.ImageIcon("./images/imagemLimpar.jpg")); 
        buttonClean.setText("Limpar Campos");
        buttonClean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCleanActionPerformed(evt);
            }
        });

        labelInformation2.setForeground(new java.awt.Color(153, 153, 153));
        labelInformation2.setText("exemplo: C:\\Users\\Administrador\\Desktop\\nomeArquivo.dat");

        panelResult.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true));

        labelMSE.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelMSE.setText("MSE:");

        labelPSNR.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelPSNR.setText("PSNR:");

        txtMSE.setEditable(false);

        txtPSNR.setEditable(false);

        labelResult.setFont(new java.awt.Font("Tahoma", 3, 12));
        labelResult.setForeground(new java.awt.Color(51, 51, 51));
        labelResult.setText("Resultados ");

        javax.swing.GroupLayout panelResultLayout = new javax.swing.GroupLayout(panelResult);
        panelResult.setLayout(panelResultLayout);
        panelResultLayout.setHorizontalGroup(
            panelResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelResultLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelResult)
                .addContainerGap(629, Short.MAX_VALUE))
            .addGroup(panelResultLayout.createSequentialGroup()
                .addGap(244, 244, 244)
                .addGroup(panelResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelResultLayout.createSequentialGroup()
                        .addComponent(labelPSNR)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPSNR, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
                    .addGroup(panelResultLayout.createSequentialGroup()
                        .addComponent(labelMSE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtMSE, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)))
                .addGap(262, 262, 262))
        );
        panelResultLayout.setVerticalGroup(
            panelResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelResultLayout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(labelResult)
                .addGap(18, 18, 18)
                .addGroup(panelResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelMSE)
                    .addComponent(txtMSE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPSNR)
                    .addComponent(txtPSNR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelResult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelSource)
                        .addGap(440, 440, 440))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelInformation)
                        .addContainerGap(431, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelEncoded)
                        .addContainerGap(525, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtSource, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 713, Short.MAX_VALUE)
                            .addComponent(txtEncoded, javax.swing.GroupLayout.DEFAULT_SIZE, 713, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelInformation2)
                        .addContainerGap(434, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(buttonClean)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 416, Short.MAX_VALUE)
                        .addComponent(buttonCodec, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelSource)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtSource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelInformation)
                .addGap(18, 18, 18)
                .addComponent(labelEncoded)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtEncoded, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelInformation2)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCodec, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonClean, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addComponent(panelResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    private void txtSourceActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void txtEncodedActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void buttonCodecActionPerformed(java.awt.event.ActionEvent evt) {
    	checkClean();
    	Dat source = null;
    	Dat encoded = null;
    	
    	try{
    		source = fileManager.loadDatFile(txtSource.getText());
    		encoded = fileManager.loadDatFile(txtEncoded.getText());
    	
    		double mse = codedManager.calculateMSE(source, encoded);
    		txtMSE.setText(String.valueOf(mse));
			double psnr = codedManager.calculatePSNR(source, encoded);
			txtPSNR.setText(String.valueOf(psnr));
		
			JOptionPane.showMessageDialog(this, "Processo Finalizado!", "Fim", JOptionPane.INFORMATION_MESSAGE);
			
		}catch(SystemException e){
    		JOptionPane.showMessageDialog(this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
    	}
    }
    
    private void checkClean(){
    	if(txtSource.getText().length() == 0 || txtEncoded.getText().length() == 0){
            JOptionPane.showMessageDialog(this, "Campo não informado",
                    "Atenção", JOptionPane.ERROR_MESSAGE);
            throw new SystemException("Campo da Tela em Branco!", null);
        }
    }

    private void buttonCleanActionPerformed(java.awt.event.ActionEvent evt) {
        txtSource.setText("");
        txtEncoded.setText("");
        txtSource.requestFocus();
    }

    public void openFrameInputScreenCalc(){
         InputScreenCalc frameInputScreenCalc = new InputScreenCalc();
         frameInputScreenCalc.setVisible(true);
         frameInputScreenCalc.setLocationRelativeTo(null);
    }

    private javax.swing.JButton buttonClean;
    private javax.swing.JButton buttonCodec;
    private javax.swing.JLabel labelEncoded;
    private javax.swing.JLabel labelInformation;
    private javax.swing.JLabel labelInformation2;
    private javax.swing.JLabel labelMSE;
    private javax.swing.JLabel labelPSNR;
    private javax.swing.JLabel labelResult;
    private javax.swing.JLabel labelSource;
    private javax.swing.JPanel panelResult;
    private javax.swing.JTextField txtEncoded;
    private javax.swing.JTextField txtMSE;
    private javax.swing.JTextField txtPSNR;
    private javax.swing.JTextField txtSource;

}
